package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.Appraise;
import com.dnjn.happiness.biz.domain.Communicate;
import com.dnjn.happiness.biz.service.IAppraiseService;
import com.dnjn.happiness.biz.service.ICommunicateService;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.system.api.RemoteTeamService;
import com.dnjn.happiness.system.api.domain.SysTeam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 周报
 */
@Tag(name = "周报", description = "周报")
@RestController
@RequestMapping("/week")
public class WeekController extends BaseController {


    @Autowired
    private IAppraiseService appraiseService;

    @Autowired
    private ICommunicateService communicateService;

    @Autowired
    private RemoteTeamService teamService;

    @Operation(description = "周报-根据部门id查询部门下的所有管理者列表-getLeaderListBydeptId")
    @PostMapping("/getLeaderListBydeptId")
    public AjaxResult getLeaderListBydeptId(@RequestParam(required = false) Long deptId) {
        if (Objects.isNull(deptId)) {
            deptId = SecurityUtils.getLoginUser().getDeptId();
        }
        List<SysTeam>  list = new ArrayList<>();
        R<List<SysTeam>> resut = teamService.getLeaderListBydeptId(deptId, SecurityConstants.INNER);
        if (StringUtils.isNotNull(resut) && StringUtils.isNotNull(resut.getData())){
            list = resut.getData();
        }
        return AjaxResult.success(list);
    }

    @Operation(description = "周报-根据userId查询该用户所有评价记录,只传fromUserId-getAppraiseListByUserId")
    @GetMapping("/getAppraisePageByFromUserId")
    public AjaxResult getAppraisePageByFromUserId(@RequestParam Long fromUserId, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Appraise appraise = new Appraise().setFromUserId(fromUserId);
        Page page = Page.of(current, size);
        Page<Appraise> list = appraiseService.getAppraisePageByFromUserId(appraise, page);
        return AjaxResult.success(list);
    }

    @Operation(description = "周报-根据userId查询该用户所有沟通记录,只传fromUserId-getAppraiseListByUserId")
    @GetMapping("/getCommunicatePageByFromUserId")
    public AjaxResult getCommunicatePageByFromUserId(@RequestParam Long fromUserId, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Communicate communicate = new Communicate().setFromUserId(fromUserId);
        List<Communicate> list = communicateService.getCommunicatePageByFromUserId(communicate);
        IPage<Communicate> page = listToPage(list, current, size);
        communicateService.getWeekScoreChange(page.getRecords());
        return AjaxResult.success(page);
    }

}
